<?php

namespace Biz\service\activity\project;

use Exception;

trait FieldChangeTrait
{

    public array $emailNotice = [];

    public function useNameOptions($arrValue, array $options, $separator = ','): string
    {
        if (!is_array($arrValue)) $arrValue = explode($separator, $arrValue);
        $name = [];
        foreach ($arrValue as $id) {
            $name[] = $options[$id] ?? $id;
        }
        return implode(',', $name);
    }
    public function filterArrayParam($param)
    {
        if (is_array($param)) {
            $param = array_filter($param);
            $param = array_values($param);
        }
        return $param;
    }
    public function wrapColor($title, $wrap = false): string
    {
        return $wrap ? "<span style=\"color: #ff0000;\">$title</span>" : $title;
    }

    public function changeCheck($fields, $extend, $info): bool
    {
        $rs = false;
        foreach ($fields as $v) {
            if (is_scalar($v[0])) $v[0] = [$v[0]];
            $change = false;
            foreach ($v[0] as $field) {
                $change = isset($extend[$field]) && isset($info[$field]) && $extend[$field] != $info[$field];
                if ($change) {
                    $rs = true;
                    break;
                }
            }
            $this->emailNotice[$v[2] ?? (implode("_", $v[0]))] = $this->wrapColor($v[1], $change);
        }
        return $rs;
    }


}